#include "linux/module.h"
#include "linux/init.h"
#include "linux/gpio.h"
#include "linux/interrupt.h"

#define GPIO_PIN 101 //101 13

static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
{
    printk(KERN_INFO "中断发生在GPIO%d\n", GPIO_PIN);
    printk(KERN_INFO "这是中断服务程序\n");
    return IRQ_HANDLED;
}

static int __init my_driver_init(void)
{
    int irq_num;
    printk("正在初始化GPIO中断驱动.\n");

    irq_num = gpio_to_irq(GPIO_PIN);
    printk("GPIO%d映射到IRQ%d\n", GPIO_PIN, irq_num);

    if (request_irq(irq_num, gpio_irq_handler, IRQF_TRIGGER_RISING, "irq_test", NULL) != 0)
    {
        printk("请求中断号失败\n");
        gpio_free(GPIO_PIN);
        return -ENODEV;
    }
    return 0;
}

static void __exit my_driver_exit(void)
{
    int irq_num = gpio_to_irq(GPIO_PIN);
    // 释放中断
    free_irq(irq_num, NULL);
    printk(KERN_INFO "GPIO Interrupt Driver exited successfully\n");
}

module_init(my_driver_init);
module_exit(my_driver_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("逮就捕");